package org.openfans.web;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.PropertyUtils;
import org.openfans.domain.Facade;
import org.openfans.domain.Fans;
import org.openfans.domain.FansFeed;
import org.openfans.domain.Feed;
import org.openfans.domain.OthersFeed;
import org.openfans.domain.TagFeed;
import org.openfans.feed.FeedManager;
import org.openfans.util.ThreadFans;
import org.openfans.web.form.FeedForm;
import org.springframework.validation.BindException;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;

public class FeedFormController extends SimpleFormController {
	private Facade facade = null;
	
	private FeedManager feedManager=null;

	public void setFacade(Facade facade) {
		this.facade = facade;
	}

	public void setFeedManager(FeedManager feedManager) {
		this.feedManager = feedManager;
	}

	public FeedFormController() {
		setCommandName("feedForm");
		setCommandClass(FeedForm.class);
	}

	protected ModelAndView onSubmit(HttpServletRequest request,
			HttpServletResponse response, Object command, BindException errors)
			throws Exception {
		FeedForm feedForm = (FeedForm) command;
		String tag = feedForm.getTagStr();
		String type = feedForm.getType();
		Fans fans = facade.getFans(ThreadFans.get().getFansId());
		Feed feed = null;

		if (feedForm.getFeedId() != 0) {// 已经存在
			feed = facade.getFeed(feedForm.getFeedId());
			PropertyUtils.copyProperties(feed, command);
			facade.save(feed);
			
		} else {
			if (type != null && !"".equals(type)) {
				if ("1".equals(type)) {
					feed = new FansFeed();
				} else if ("2".equals(type)) {
					feed = new TagFeed();
					((TagFeed) feed).setTag(facade.getTagByTitle(tag));
				} else if ("3".equals(type)) {
					feed = new OthersFeed();
				}
			} else {
				feed = new Feed();
			}
			PropertyUtils.copyProperties(feed, command);
			feed.setLastDate(new Date(100, 1, 1));

			if (feed instanceof TagFeed) {
				// 需将feed存入数据库,再将其加入tag的feed列表中
				((TagFeed) feed).getTag().addFeed((TagFeed) feed);
			}
			facade.saveFeed(fans, feed);
		}

		feedManager.refreshFeed(feed);
		return new ModelAndView("redirect:listFeedItem.html");
	}

	protected Object formBackingObject(HttpServletRequest request)
			throws Exception {
		ThreadFans.authorize();
		String feedId = request.getParameter("id");
		if ((feedId != null) && !feedId.equals("0")) {
			Feed feed = facade.getFeed(Integer.parseInt(feedId));
			FeedForm feedForm = new FeedForm();
			PropertyUtils.copyProperties(feedForm, feed);
			if (feed instanceof FansFeed) {
				feedForm.setType("1");
			} else if (feed instanceof TagFeed) {
				feedForm.setType("2");
				feedForm.setTagStr(((TagFeed) feed).getTag().getTitle());
			} else if (feed instanceof OthersFeed) {
				feedForm.setType("3");
			}
			return feedForm;
		}
		return new FeedForm();
	}
}
